<?php

class Admin_BaseController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function indexAction()
    {
        $this->_forward("show-site");
    }
    
    public function saveAction()
    {
        $viewResult = new Ext_View_Result();
        
        App_Model_DbTable_Settings::getInstance()->AddSettingsAllRow($this->_request->getPost());
        
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }

    public function showAclAction()
    {
        $_appModelDbTableAclRole = App_Model_DbTable_Acl_Role::getInstance();
        
        $_settings = $this->_getSettingAll();
        
        $viewForm = new Ext_View_Form_Admin_Base_Acl();
        
        $roleAllArray = $_appModelDbTableAclRole->getAll();
        $_options = array();
        for($i=0;$i<count($roleAllArray);$i++)
        {
            $__roleId = $roleAllArray[$i]['id'];
            $_options[$__roleId] = $roleAllArray[$i]['roleName'];
        }
        $viewForm->getElement('userDefaultRoleId')->addMultiOption(null, null);
        $viewForm->getElement('userDefaultRoleId')->addMultiOptions($_options);
        if(isset($_settings['userDefaultRoleId']))
        {
            $viewForm->getElement('userDefaultRoleId')->setValue($_settings['userDefaultRoleId']);
        }
        
        $this->view->form = $viewForm;
    }
    
    public function showAccountAction()
    {
        $settings = $this->_getSettingAll();
        
        $viewForm = new Ext_View_Form_Admin_Base_Account();
        if(isset($settings['userAccountUsername']))
        {
            $viewForm->getElement('userAccountUsername')->setValue($settings['userAccountUsername']);
        }
        
        $this->view->form = $viewForm;
    }
    
    public function showSiteAction()
    {
        $settings = $this->_getSettingAll();
        
        $viewForm = new Ext_View_Form_Admin_Base_Site();
        if(isset($settings['siteName'])){
            $viewForm->getElement('siteName')->setValue($settings['siteName']);
        }
        if(isset($settings['siteDescription'])){
            $viewForm->getElement('siteDescription')->setValue($settings['siteDescription']);
        }
        if(isset($settings['siteContact'])){
            $viewForm->getElement('siteContact')->setValue($settings['siteContact']);
        }
        if(isset($settings['siteBeianhao'])){
            $viewForm->getElement('siteBeianhao')->setValue($settings['siteBeianhao']);
        }
        
        $this->view->form = $viewForm;
    }
    
    public function showStyleAction()
    {
        $settings = $this->_getSettingAll();
        $viewForm = new Ext_View_Form_Admin_Base_Style();
        
        $options = array();
        $themeDir = APPLICATION_PATH."/views/";
        if (is_dir($themeDir)) {
            if ($dh = opendir($themeDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('defaultTheme')->addMultiOptions($options);
        if(isset($settings['defaultTheme']))
        {
            $viewForm->getElement('defaultTheme')->setValue($settings['defaultTheme']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/base";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('styleBase')->addMultiOptions($options);
        if(isset($settings['styleBase']))
        {
            $viewForm->getElement('styleBase')->setValue($settings['styleBase']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/pagination";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('stylePagination')->addMultiOptions($options);
        if(isset($settings['stylePagination']))
        {
            $viewForm->getElement('stylePagination')->setValue($settings['stylePagination']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/jqueryui";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('styleJqueryui')->addMultiOptions($options);
        if(isset($settings['styleJqueryui']))
        {
            $viewForm->getElement('styleJqueryui')->setValue($settings['styleJqueryui']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/blueprint";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('styleBlueprint')->addMultiOptions($options);
        if(isset($settings['styleBlueprint']))
        {
            $viewForm->getElement('styleBlueprint')->setValue($settings['styleBlueprint']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/imageGallery";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('styleImageGallery')->addMultiOptions($options);
        if(isset($settings['styleImageGallery']))
        {
            $viewForm->getElement('styleImageGallery')->setValue($settings['styleImageGallery']);
        }
        
        $options = array();
        $styleDir = ROOT_PATH."/public/ui/style/syntaxhighlighter";
        if (is_dir($styleDir)) {
            if ($dh = opendir($styleDir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file == '.' || $file == '..'){
                        continue;
                    }

                    $options[$file] = $file;
                }
                closedir($dh);
            }
        }
        $viewForm->getElement('styleSyntaxhighlighter')->addMultiOptions($options);
        if(isset($settings['styleSyntaxhighlighter']))
        {
            $viewForm->getElement('styleSyntaxhighlighter')->setValue($settings['styleSyntaxhighlighter']);
        }
        
        $this->view->form = $viewForm;
    }
    
    public function showArticleAction()
    {
        $settings = $this->_getSettingAll();
        $viewForm = new Ext_View_Form_Admin_Base_Article();
        
        if(isset($settings['allowUploadImage'])){
            $viewForm->getElement('allowUploadImage')->setValue($settings['allowUploadImage']);
        }
        if(isset($settings['allowShareVideo'])){
            $viewForm->getElement('allowShareVideo')->setValue($settings['allowShareVideo']);
        }
        if(isset($settings['allowShareMusic'])){
            $viewForm->getElement('allowShareMusic')->setValue($settings['allowShareMusic']);
        }
        
        $this->view->form = $viewForm;
    }
    
    private function _getSettingAll()
    {
        $_appModelDbTableSettings = new App_Model_DbTable_Settings();
        
        $settingsAllRow = $_appModelDbTableSettings->getSettingsAll();
        $settings = array();
        for($i=0;$i<count($settingsAllRow);$i++)
        {
            $settings[$settingsAllRow[$i]['k']] = $settingsAllRow[$i]['v'];
        }
        $this->view->settings = $settings;
        
        return $settings;
    }
}

